package com.trkj.service.wdm;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wdm.DutyDAO;
import com.trkj.pojo.Duty;
import com.trkj.service.ServiceException;

@Service
@Transactional(rollbackFor = Exception.class)
public class DutyService {
	@Autowired
	private DutyDAO dutydao;

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<Duty> queryByPage(int page, int rows) {
		return dutydao.queryByNaPage(new PageRequest(page - 1, rows,Direction.DESC,"dutyCreatordate"));
	}

	/**
	 * 获取总记录条数
	 * 
	 * @return
	 */
	public long queryByCount() {
		return dutydao.count();

	}

	// 新增
	public void definedutyadd( String dutyName,String dutyPerson) throws ServiceException {
		try {
			Duty o = new Duty();
			o.setDutyName(dutyName);
			o.setDutyPerson(dutyPerson);
			Date date = new Date();
			o.setDutyCreatordate(new Timestamp(date.getTime()));
			o.setDutyUpdatedate(new Timestamp(date.getTime()));
			dutydao.save(o);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	/**
	 * 编辑
	 */
	public void updatestate(Long dutyId ,String dutyName,String dutyPerson){
		try {
			Duty rea = dutydao.findOne(dutyId);
			rea.setDutyName(dutyName);
			rea.setDutyPerson(dutyPerson);
			rea.setDutyUpdatedate(new Timestamp(new Date().getTime()));
			dutydao.save(rea);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	// 删除
	public void definedutyremove(Long dutyId) {
		dutydao.delete(dutyId);
	}
	/**
	 * 加载
	 * @return
	 */
	public Duty loadReasonsLeaving(Long bptId) {
		return dutydao.findOne(bptId);
	}
	// 搜索
	public List<Duty> filedByName(String dutyName,int page,int rows) {

		return dutydao.query_By_idorname("%" + dutyName + "%",new PageRequest(page-1, rows));
	}
	/**
	 * 获取总记录条数
	 * 
	 * @return
	 */
	public long count(String text) {
		return dutydao.count("%" + text + "%");

	}
}
